package com.goldsprite.microhorigame.ecs.fsms.states;
import java.util.*;

public class EntityContinuousAttackState extends EntityAttackState {
	private int curIndex, curAIndex;
	private List<String> animNames = new ArrayList<>();
	private boolean keyUp, nexted;

	@Override
	public void enter() {
		super.enter();
		if(nexted) fsm.getInputs().setKeyAttack(false);
		nexted = false;
		keyUp = false;
		curAIndex = curIndex;
	}

	@Override
	public void exit() {
		super.exit();
		if(nexted) fsm.getInputs().setKeyAttack(true);
	}

	public EntityContinuousAttackState setAnimNames(String... animNames) {
		this.animNames.clear();
		this.animNames.addAll(Arrays.asList(animNames));
		return this;
	}

	@Override
	public String getCurAnimName() {
		return animNames.get(curIndex);
	}

	public void nextAnim() {
		curIndex = ++curIndex % animNames.size();
	}

	@Override
	public void running() {
		super.running();

		if(!fsm.getInputs().getKeyAttack()) keyUp = true;

		if(keyUp && fsm.getInputs().getKeyAttack() && !nexted){
			nexted = true;
			nextAnim();
		}
	}


	public boolean isCancel() {
		int playingIndex = fsm.getAnims().curAnimFrameIndex();
		return fsm.getInputs().isMoving() && playingIndex > attackFrames.get(curAIndex);
	}
}
